home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / toogl / perlassoc.h < prev    next >
C/C++ Source or Header  |  1996-11-11  |  4KB  |  148 lines

  1. /*
  2.  * Copyright (c) 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or publicity relating 
  9.  * to the software without the specific, prior written permission of 
  10.  * Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, 
  14.  * ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  15.  *
  16.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, 
  17.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER 
  18.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF 
  19.  * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT 
  20.  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. /*
  24.  * Version 1.4
  25.  * Feeble attempt to duplicate perl associative arrays
  26.  * So feeble I won't even call it PerlAssoc!
  27.  * Anyway the key can only be a string, the value can be anything.
  28.  * Written by Jim Morris,  jegm@sgi.com
  29.  */
  30. #ifndef    _PERLASSOC_H
  31. #define _PERLASSOC_H
  32.  
  33. #include <iostream.h>
  34. #include "perlclass.h"
  35.  
  36. template<class T>
  37. class Binar
  38. {
  39. private:
  40.     PerlString k;
  41.     T v;
  42.  
  43. public:
  44.     Binar(PerlString a, T b) : k(a), v(b){}
  45.     Binar(PerlString a) : k(a){}
  46.     Binar(){}
  47.     
  48.     Binar<T>& operator=(const Binar<T>& n){ k= n.k; v= n.v; return *this; }
  49.     PerlString& key(void){ return k; }
  50.     const PerlString& key(void) const { return k; }
  51.     T& value(void){ return v; }
  52.     const T& value(void) const { return v; }
  53.     int operator==(const Binar<T>& b) const{return ((k == b.k) && (v == b.v));}    
  54.     int operator<(const Binar& b) const {return v < b.v;} // to keep sort quiet
  55. };
  56.  
  57. template<class T>
  58. class Assoc
  59. {
  60. private:
  61.     PerlList<Binar<T> > dat;
  62.     Binar<T> def;
  63.     
  64. public:
  65.     Assoc():def(""){}
  66.     Assoc(PerlString dk, T dv) : def(dk, dv){}
  67.  
  68.     int scalar(void) const { return dat.scalar(); }
  69.     
  70.     PerlStringList keys(void);
  71.     PerlList<T> values(void);
  72.     
  73.     int isin(const PerlString& k) const;
  74.     T adelete(const PerlString& k);
  75.         
  76.     T& operator()(const PerlString& k);
  77.     Binar<T>& operator[](int i){ return dat[i]; }
  78. };
  79.  
  80. template<class T>
  81. PerlStringList Assoc<T>::keys(void)
  82. {
  83.     PerlStringList r;
  84.     for(int i=0;i<dat.scalar();i++)
  85.     r.push(dat[i].key());
  86.     return r;
  87. }    
  88.  
  89. template<class T>
  90. PerlList<T> Assoc<T>::values(void)
  91. {
  92.     PerlList<T> r;
  93.     for(int i=0;i<dat.scalar();i++)
  94.     r.push(dat[i].value());
  95.     return r;
  96. }
  97.  
  98. template<class T>
  99. T& Assoc<T>::operator()(const PerlString& k)
  100. {
  101.     for(int i=0;i<dat.scalar();i++){
  102.     if(k == dat[i].key()) return dat[i].value();
  103.     }
  104.     
  105.     dat.push(Binar<T>(k, def.value()));
  106.     return dat[i].value();
  107. }
  108.  
  109. template<class T>
  110. T Assoc<T>::adelete(const PerlString& k)
  111. {
  112.     for(int i=0;i<dat.scalar();i++){
  113.     if(k == dat[i].key()){
  114.         T r= dat[i].value();
  115.         dat.splice(i, 1);
  116.         return r;
  117.     }
  118.     }
  119.     
  120.     return def.value();
  121. }
  122.  
  123. template<class T>
  124. int Assoc<T>::isin(const PerlString& k) const
  125. {
  126.     for(int i=0;i<dat.scalar();i++){
  127.     if(k == dat[i].key()) return i+1;
  128.     }
  129.     return 0;
  130. }
  131.  
  132. template<class T>
  133. ostream& operator<<(ostream& os, Binar<T>& a)
  134. {
  135.     os << "(" << a.key() << ", " << a.value() << ")";
  136.     return os;
  137. }
  138.  
  139. template<class T>
  140. ostream& operator<<(ostream& os, Assoc<T>& a)
  141. {
  142.     for(int i=0;i<a.scalar();i++){
  143.     os << "[" << i << "] " << a[i] << endl;
  144.     }
  145.     return os;
  146. }
  147. #endif
  148.